using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace SPPivotViewer.Features.SPPivotViewerFeature
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("35b20bb3-47f0-4087-bab7-0d022049107e")]
    public class SPPivotViewerFeatureEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        private const string MODS_OWNER = "SPPivotViewerFeature";
        private const string MOD_PATH = "configuration/system.webServer/handlers";
        private const string MOD_NAME = @"add[@path=""{0}""]";
        private const string MOD_VALUE = @"<add name=""{0}"" verb=""{1}"" path=""{2}"" type=""{3}""/>";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            webApp.WebConfigModifications.Add(AddHttpHandler(MODS_OWNER, "cxml", "*", "*.cxml", "SPPivotViewer.HttpHandler.SPCxmlHandler, SPPivotViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab999e78ba71c798"));
            webApp.WebConfigModifications.Add(AddHttpHandler(MODS_OWNER, "dzc", "GET", "*.dzc", "SPPivotViewer.HttpHandler.SPDzcHandler, SPPivotViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab999e78ba71c798"));
            webApp.WebConfigModifications.Add(AddHttpHandler(MODS_OWNER, "dzi", "GET", "*.dzi", "SPPivotViewer.HttpHandler.SPDziHandler, SPPivotViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab999e78ba71c798"));
            webApp.WebConfigModifications.Add(AddHttpHandler(MODS_OWNER, "deepZoom", "GET", "*/dzi/*_files/*/*_*.jpg", "SPPivotViewer.HttpHandler.SPDeepZoomImageHandler, SPPivotViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab999e78ba71c798"));
            webApp.WebConfigModifications.Add(AddHttpHandler(MODS_OWNER, "ImageTile", "GET", "*_files/*/*_*.jpg", "SPPivotViewer.HttpHandler.SPImageTileHandler, SPPivotViewer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab999e78ba71c798"));
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

        }

        private SPWebConfigModification AddHttpHandler(string Owner,string name, string verb, string path, string type)
        {
            SPWebConfigModification mod = new SPWebConfigModification(string.Format(MOD_NAME, path), MOD_PATH);
            mod.Value = String.Format(MOD_VALUE,name, verb, path, type);
            mod.Owner = Owner;
            mod.Sequence = 0;
            mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            return mod;
        }

        

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            Collection<SPWebConfigModification> mods = webApp.WebConfigModifications;

            int startCount = mods.Count;
            for (int c = startCount - 1; c >= 0; c--)
            {
                SPWebConfigModification mod = mods[c];
                if (mod.Owner == MODS_OWNER)
                    mods.Remove(mod);
            }

            if (startCount > mods.Count)
            {
                webApp.Update();
                webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }

        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
